package ProjectManagement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import com.spm.pojo.StudentChooseCourse;
import com.spm.pojo.Teacher;
import com.spm.utils.StudentGradeMapperUtil;
import com.spm.utils.StudentMapperUtil;

/**
 * 逻辑处理及页面跳转
 * @author DY1101shaoyuxian
 *
 */
@WebServlet(name = "PreServlet",value = "/pre")
public class PreServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    String method;
    String Sid;
    StudentGradeMapperUtil studentGradeMapperUtil=new StudentGradeMapperUtil();
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        method = (String) request.getParameter("method");
        Sid= request.getParameter("Sid");
        if("preupdatetepwd".equals(method)){
            preupdatetepwd(request, response);
        }else if("courseshow".equals(method)) {
            courseshow(request, response);
        }else if ("OneGradeQuery".equals(method)){
            OneGradeQuery(request,response);
        }
        else if ("AllGradeQuery".equals(method)){
            AllGradeQuery(request,response);
        }else if ("GradeEnter".equals(method)){
            GradeEnter(request,response);
        }else if("Tcourseshow".equals(method)){
            Tcourseshow(request,response);
        }else if("confirm".equals(method)){
            confirm(request,response);
        }else if("delete".equals(method)){
            delete(request,response);
        }
        else if(method==null){
            updatetepwd(request, response);
        }
    }
    public void confirm(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        HttpSession session=request.getSession();
        Teacher teacher=(Teacher) session.getAttribute("teacher");
        String Tid=teacher.getTid();
        String courseid= session.getAttribute("courseid").toString();
        System.out.println(Sid);
        System.out.println(Tid);
        System.out.println(courseid);
        studentGradeMapperUtil.updateChooseState(Sid,Tid,courseid);

        String mainPage = "teacherclassselection.jsp";
        List<StudentChooseCourse> studentChooseCourses = new StudentMapperUtil().selectClassChooseStudentListByTnoAndCno(teacher.getTid(), courseid);
        request.setAttribute("studentChooseCourses",studentChooseCourses);
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    } public void delete(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        HttpSession session=request.getSession();
        Teacher teacher=(Teacher) session.getAttribute("teacher");
        String Tid=teacher.getTid();
        String courseid=(String) session.getAttribute("courseid");
        studentGradeMapperUtil.deleteChooseRecordBySidAndCnoAndTid(Sid,request.getSession().getAttribute("courseid").toString(),Tid);
        List<StudentChooseCourse> studentChooseCourses = new StudentMapperUtil().selectClassChooseStudentListByTnoAndCno(teacher.getTid(), courseid);
        request.setAttribute("studentChooseCourses",studentChooseCourses);
        String mainPage = "teacherclassselection.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
    public void Tcourseshow(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String mainPage = "Tcourseshow.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
    public void OneGradeQuery(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String mainPage = "OneGradeQuery.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
    public void AllGradeQuery(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String mainPage = "AllGradeQuery.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
    public void GradeEnter(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String mainPage = "GradeEnter.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
    /**
     * 确认学生选课-实现页面跳转
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void courseshow(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String mainPage = "courseshow.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
    /**
     * 更改管理员密码-实现页面跳转
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void preupdatetepwd(HttpServletRequest request,
                               HttpServletResponse response) throws ServletException, IOException {
        String mainPage = "updatepwd.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
    /**
     * 更改管理员密码
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void updatetepwd(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        String username=(String)session.getAttribute("username");
        String repassword=request.getParameter("repassword");
        Teacher teacher=new Teacher(username,repassword);
        TeacherDaoImpl userdao=new TeacherDaoImpl();
        int a=userdao.updateUserPassWord(teacher);
        if(a!=0){
            String mainPage = "updatepwdSuccess.jsp";
            request.setAttribute("mainPage", mainPage);
        }else{
            request.setAttribute("error", "更新失败");
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }


}

